/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.Actions;

import java.io.Serializable;

import controller.Forms.AccountForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Account;
import model.Erro;
import model.User;
import dao.AccountDAO;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author adriano
 */
public class RegisterNewAccountAction extends Action implements Serializable{

	final static Logger logger = Logger.getLogger(RegisterNewAccountAction.class);
    @Override
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
            try {
            User u = (User) request.getSession().getAttribute("User");

            if (u == null) {
                request.getSession().setAttribute("ErroMSG", Erro.erroMsg[Erro.NAO_AUTENTICADO]);
                
                return mapping.findForward("erroAlias");
                
            } else {
            	
                AccountForm accountForm = (AccountForm) form;
                Account account = new Account();
                
                
                account.setLstGrupoDeContas(accountForm.getLstGrupoDeContas());
                account.setLstSubGrupoDeContas(accountForm.getLstSubGrupoDeContas());
                account.setLstPlanoDeContas(accountForm.getLstPlanoDeContas());
                account.setLstStatus(accountForm.getLstStatus());
                account.setTxtDescricao(accountForm.getTxtDescricao());
                
                
                
                
                AccountDAO accountDAO = new AccountDAO();

                accountDAO.create(account);
                accountDAO.closeDAO();

                return mapping.findForward("homeAlias");
            }

        } catch (Exception e) {
            logger.error("CADASTRAR_CONTADORACTION -",e);
            return mapping.findForward("erroAlias");
        }

        
        

    }
    
    

}
